$(document).ready(function(){
    login_register.Init();
});


var login_register = {
	
	// Bind click to login/register button
    Init: function(){
        this.user_login_bind();
        this.user_register();
		$('#username').focus();
    },
    user_login_bind : function(){
		var $this = this;
		$('.login_password').keyup(function(e) {
			   if(e.keyCode == 13 && !$this.disableEnter) 
			   	 $this.user_login();
			   else if($this.disableEnter)
			   	  $this.disableEnter = false;	
   		});
        $("#button_login").click(function(){
				$this.user_login();
		});
	},
    user_login: function(){
			this.disableEnter = false;
            
            var useremail = $('.login_email input').val();
            var userpassword = hex_md5($('.login_password input').val());
            var button_status = $('.login_remember input:checkbox:checked').val();
			
			var $this = this;
			
			callApi('userApi', 'login', 'POST', {email: useremail, password: userpassword}, function(resultObj) {
				if (resultObj.userId >= 0) {
                    window.location = "/project-rev-switch.html";
                } else {
					$this.disableEnter = true;
                    alert('The username or password is incorrect. Verify that CAPS LOCK is\n  not on, and then retype the current username and password.');
                }
			});

    },
    
    /*user register function*/
    user_register: function(){
        $("#button_register").click(function(){
            var str_firstname = $('.register_firstname input').val();
            var str_lastname = $('.register_lastname input').val();
            var str_email = $('.register_email input').val();
            var str_password = $('.register_password input').val();
            var str_repassword = $('.register_repassword input').val();

            if (str_firstname == ""||str_lastname == ""||str_email == ""||str_password == ""||str_password != str_repassword) {
                alert("You must fill in all of the fields.");
				return;
            }
			
            if (str_password != str_repassword) {
                alert("Password does not match!");
				return;
            }
			
            var params = { 
				email: str_email,
                password: hex_md5(str_password),
                firstName: str_firstname,
                lastName: str_lastname,
            };
			
			callApi('userApi', 'register', 'POST', params, function(resultObj) {
                if (resultObj.success == true) /* access sucessfully */ {
                    callApi('userApi', 'login', 'POST', {email: params.email, password: params.password}, function(resultObj) {
						if (resultObj.userId >= 0) {
		                    window.location = "/project-rev-switch.html";
		                } else {
		                    alert('Unable to login. Please try again.');
		                }
					});
                }
                else {
                    alert("unsucessful");
                }
			});
        })
    },
}
